package com.rabbitmq.rabbitmqspring.controller;

import com.rabbitmq.rabbitmqspring.constant.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

@RequestMapping("/producer")
@RestController
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    private static final Integer MESSAGE_COUNT = 10;
    @Autowired
    private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

    @RequestMapping("/work")
    public String workQueue() {

        for (int i = 0; i < MESSAGE_COUNT; i++) {
            String message = "word queue" + i;
            rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE, message);
        }

        return "发送成功";
    }

    @RequestMapping("/fanout")
    public String fanout() {
        for (int i = 0; i < MESSAGE_COUNT; i++) {
            String message = "fanout " + i;
            //发布订阅模式的 routingKey 为 ""
            rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE, "", message);
        }
        return "发送成功";
    }

    @RequestMapping("/direct")
    public String direct() {
        String message1 = "direct, routingKey: " + Constants.DIRECT_ROUTINGKEY1;
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE, Constants.DIRECT_ROUTINGKEY1, message1);

        String message2 = "direct, routingKey: " + Constants.DIRECT_ROUTINGKEY2;
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE, Constants.DIRECT_ROUTINGKEY2, message2);

        return "发送成功";
    }

    @RequestMapping("/topic")
    public String topic() {
        String message1 = "topic, routingKey1: " + "b.a.c";
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE, "b.a.b", message1); // 1

        String message2 = "topic, routingKey1: " + "c.b.a";
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE, "c.b.a", message2); //2

        String message3 = "topic, routingKey1: " + "c.wvea.awegewt.qrg";
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE, "c.wvea.awegewt.qrg", message3); //2

        return "发送成功";
    }
}
